// component/goods_list/goods_list.js
var app = getApp();
Component({
  properties: {
    showType: { // 显示类型，play表示玩，exchange表示兑换，catched表示抓中商品，exchanged表示兑换商品,
      type: String,
      value: 'default',
    },
    goodsData: {
      type: Array,
      value: [],
    },
  },

  data: {
  },
  methods: {
    /**
     * 表单提交事件
     */
    formSubmit: function (e) {
      this.viewGoods(e.detail.value.index);
      this.collectFormid(e.detail.formId);
    },

    /**
     * 查看商品
     */
    viewGoods: function (index) {
      var target = this.data.goodsData[index];
      var id = target.goods_id;
      var myEventDetail = {
        id: id,
        target: target,
      }
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('view', myEventDetail, myEventOption);
    },

    /**
     * 收集form_id
     */
    collectFormid: function (formid) {
      if (formid == 'the formId is a mock one') {
        return;
      }
      var formid_Arr = wx.getStorageSync('formid') || [];
      formid_Arr.push(formid);
      wx.setStorageSync('formid', formid_Arr);
      if (formid_Arr.length >= 5) {
        app.Ajax({
          url: '/api/index.php?app=group&act=form',
          method: 'POST',
          data: {
            form_id: formid_Arr.join(',')
          },
          success: (res) => {
            wx.setStorageSync('formid', []);
          }
        })
      } else {
        wx.setStorageSync('formid', formid_Arr);
      }
    },
  }
})